package cn.kongwg.service.impl;

import cn.kongwg.bean.Book;
import cn.kongwg.bean.Page;
import cn.kongwg.dao.BookDao;
import cn.kongwg.dao.impl.BookDaoImpl;
import cn.kongwg.service.BookService;

import java.util.List;

/**
 * @author kongwg
 * @create 2021-01-26 10:20
 */
public class BookServiceImpl implements BookService {
    private BookDao dao = new BookDaoImpl();

    @Override
    public void addBook(Book book) {
        dao.addBook(book);
    }

    @Override
    public void delectBook(Integer id) {
        dao.deleteBook(id);
    }

    @Override
    public void updateBook(Book book) {
        dao.updateBook(book);
    }

    @Override
    public Book queryByid(Integer id) {

        return dao.queryBookById(id);
    }

    @Override
    public List<Book> queryList() {

        return dao.queryForAll();
    }


    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page page = new Page();


        page.setPageSize(pageSize);

        Integer pageTotalCount = dao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);

        Integer begin = (pageNo - 1) * pageSize;
        List<Book> books = dao.queryForPageItem(begin, pageSize);

        page.setItem(books);

        return page;
    }

    @Override
    public Page<Book> pageByPrice(Integer pageNo, Integer pageSize, Integer min, Integer max) {
        Page page = new Page();


        page.setPageSize(pageSize);

        Integer pageTotalCount = dao.queryForPageTotalCountByPrice(min, max);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);

        Integer begin = (pageNo - 1) * pageSize;
        List<Book> books = dao.queryForPageItemsByPrice(begin, pageSize,min,max);

        page.setItem(books);

        return page;
    }
}
